package com.foosun.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.foosun.dao.WxMessageDao;
import com.foosun.entity.wechat.WxMessage;
import com.foosun.entity.wechat.WxUser;
import com.foosun.util.wechat.WxUserUtil;

/**
 * 消息管理
 * 2014-10-09
 */
@Service
public class WxMessageService {
	
	@Autowired
	private WxAccessTokenService wxAccessTokenService;
	
	@Autowired
	private WxMessageDao wxMessageDao;
	
	
	public void saveWxMess(WxMessage wm) {
		wxMessageDao.saveWxMess(wm);
	}
	
	public WxUser getWxUser(String openid) {
		return wxMessageDao.getWxUser(openid);
	}

	/**
	 * 根据openid，关注时间来确定是否已添加过此关注者
	 * @param openid
	 * @param createtime
	 * @return
	 */
	public int getUserByOpenid(String openid,long createtime) {
		return wxMessageDao.getUserByOpenid(openid, createtime);
	}
	
	/**
	 * 保存关注者
	 * @param user
	 */
	public void saveUser(WxUser user) {
		wxMessageDao.saveUser(user);
	}
	
	/**
	 * 删除关注者
	 * @param openid
	 */
	public void deleteUser(String openid) {
		wxMessageDao.deleteUser(openid);
	}
	
	
	/**
	 * 回复客服消息
	 * @param content
	 * @param openid
	 * @return
	 */
	public String sendKefuMess(String content,String openid) {
		String mesg = "ok";
		String params = "{\"touser\":\""+openid+"\","
				+ "\"msgtype\":\"text\","
				+ "\"text\": { \"content\":\""+content+"\"}"
			+ "}";
		try {
			String accessToken = wxAccessTokenService.get("wx6eb63384bd102d9c");					
			mesg = WxUserUtil.customerMessage(params, accessToken);
		} catch (Exception e) {
			e.printStackTrace();
			return "发送失败";
		}
		return mesg;
	}


}
